package com.shensi.adminCollect.service.customer.impl;

import com.shensi.adminCollect.common.utils.DateUtils;
import com.shensi.adminCollect.domain.customer.CustomerCardSetting;
import com.shensi.adminCollect.domain.customer.CustomerCardSettingDetail;
import com.shensi.adminCollect.mapper.customer.CustomerCardSettingDetailMapper;
import com.shensi.adminCollect.mapper.customer.CustomerCardSettingMapper;
import com.shensi.adminCollect.service.customer.ICustomerCardSettingService;
import com.shensi.common.core.domain.model.LoginUser;
import com.shensi.common.core.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 客户电子会员卡充值优惠设置Service业务层处理
 *
 * @author qujia
 * @date 2021-04-06
 */


@Service
public class CustomerCardSettingServiceImpl implements ICustomerCardSettingService
{
    @Autowired
    private CustomerCardSettingMapper customerCardSettingMapper;
    @Autowired
    private CustomerCardSettingDetailMapper customerCardSettingDetailMapper;

    /**
     * 查询客户电子会员卡充值优惠设置
     *
     * @param id 客户电子会员卡充值优惠设置ID
     * @return 客户电子会员卡充值优惠设置
     */
    @Override
    public CustomerCardSetting selectCustomerCardSettingById(Long id)
    {
        return customerCardSettingMapper.selectCustomerCardSettingById(id);
    }

    /**
     * 查询客户电子会员卡充值优惠设置列表
     *
     * @param customerCardSetting 客户电子会员卡充值优惠设置
     * @return 客户电子会员卡充值优惠设置
     */
    @Override
    public List<CustomerCardSetting> selectCustomerCardSettingList(CustomerCardSetting customerCardSetting)
    {

        return customerCardSettingMapper.selectCustomerCardSettingList(customerCardSetting);
    }

    /**
     * 查询油站电子卡规则设置
     * @param customerCardSetting
     * @return
     */
    @Override
    public CustomerCardSetting selectCustomerCardSetting(CustomerCardSetting customerCardSetting)
    {
        CustomerCardSetting setting=customerCardSettingMapper.selectCustomerCardSetting(customerCardSetting);
        if(setting!=null){
            CustomerCardSettingDetail detail = new CustomerCardSettingDetail();
            detail.setParentId(setting.getId());
            detail.setDelFlag("0");
            List<CustomerCardSettingDetail> detailList =customerCardSettingDetailMapper.selectCustomerCardSettingDetailList(detail);
            if(detailList==null ){
                CustomerCardSettingDetail cardSettingDetail = new CustomerCardSettingDetail();
                detailList.add(cardSettingDetail);
            }
            setting.setDetailList(detailList);
        }else{
            setting =new  CustomerCardSetting();
            setting.setIsGradeSetting("1");
            setting.setCardEnabledFlag("0");
            setting.setIsDiscountCoupon("1");
            setting.setIsMarket("1");
            setting.setEnjoyIntegralMultiple("1");
            setting.setPresentScale("0");
            setting.setDiscountTimeSetting("0");
            setting.setDiscountTime("");
            setting.setIsLabel("0");
            setting.setManualChangeWxNotifyFlag("0");
            List<CustomerCardSettingDetail> detailList =new ArrayList<>();
            setting.setDetailList(detailList);
        }
        return setting;
    }

    /**
     * 新增客户电子会员卡充值优惠设置
     *
     * @param customerCardSetting 客户电子会员卡充值优惠设置
     * @return 结果
     */
    @Override
    public int insertCustomerCardSetting(CustomerCardSetting customerCardSetting)
    {
        LoginUser currentUser = SecurityUtils.getLoginUser();
        customerCardSetting.setCreateTime(DateUtils.getNowDate());
        customerCardSetting.setCreateBy(currentUser.getUser().getUserId()+"");
        int i =customerCardSettingMapper.insertCustomerCardSetting(customerCardSetting);
        if(customerCardSetting.getDetailList()!=null && customerCardSetting.getDetailList().size()>0){
            List<CustomerCardSettingDetail> list = customerCardSetting.getDetailList();
            for(CustomerCardSettingDetail detil:list){
                detil.setDelFlag("0");
                detil.setParentId(customerCardSetting.getId());
                detil.setCreateBy(String.valueOf(currentUser.getUser().getUserId()));
                detil.setCreateTime(DateUtils.getNowDate());
                customerCardSettingDetailMapper.insertCustomerCardSettingDetail(detil);
            }
        }
        return i;
    }

    /**
     * 修改客户电子会员卡充值优惠设置
     *
     * @param customerCardSetting 客户电子会员卡充值优惠设置
     * @return 结果
     */
    @Override
    public int updateCustomerCardSetting(CustomerCardSetting customerCardSetting)
    {
        LoginUser currentUser = SecurityUtils.getLoginUser();
        int i  =customerCardSettingMapper.updateCustomerCardSetting(customerCardSetting);
        CustomerCardSettingDetail detil = new CustomerCardSettingDetail();
        detil.setParentId(customerCardSetting.getId());
//        TODO 删除是不对的 想删除手动删除
//        detil.setDelFlag("1");
//        customerCardSettingDetailMapper.updateDetail(detil);
        if(customerCardSetting.getDetailList()!=null && customerCardSetting.getDetailList().size()>0){
            List<CustomerCardSettingDetail> list = customerCardSetting.getDetailList();
            for(CustomerCardSettingDetail settingDetil:list){
                settingDetil.setDelFlag("0");
                settingDetil.setId(null);
                settingDetil.setParentId(customerCardSetting.getId());
                settingDetil.setCreateTime(DateUtils.getNowDate());
                settingDetil.setCreateBy(currentUser.getUser().getUserId()+"");
                customerCardSettingDetailMapper.insertCustomerCardSettingDetail(settingDetil);
            }
        }
        return i;
    }

    /**
     * 批量删除客户电子会员卡充值优惠设置
     *
     * @param ids 需要删除的客户电子会员卡充值优惠设置ID
     * @return 结果
     */
    @Override
    public int deleteCustomerCardSettingByIds(Long[] ids)
    {
        return customerCardSettingMapper.deleteCustomerCardSettingByIds(ids);
    }

    /**
     * 删除客户电子会员卡充值优惠设置信息
     *
     * @param id 客户电子会员卡充值优惠设置ID
     * @return 结果
     */
    @Override
    public int deleteCustomerCardSettingById(Long id)
    {
        return customerCardSettingMapper.deleteCustomerCardSettingById(id);
    }

    @Override
    public String getCardFlagById(Long enterpriseId) {
        return customerCardSettingMapper.selectCardFlagById(enterpriseId);
    }

}
